<template>
	<div>
		<so-header :title="title"></so-header>
		<mt-button type="primary" size="large" @click="getPicture">添加图片</mt-button>
		<mt-button type="primary" size="large" @touchstart.native="recording" @touchend.native="stopRecording">按住录音</mt-button>
		<mt-button type="primary" size="large" @click="getPosition">获取位置</mt-button>
		<so-footer></so-footer>
	</div>
</template>
<script>
import soHeader from 'components/header'
import soFooter from 'components/footer'
export default {
	data() {
		return {
			title: 'cordva测试'
		}
	},
	components: {
		soHeader,
		soFooter
	},
	methods: {
		//获取图片
		getPicture(){
			console.log("AAA")
			try{
				navigator.camera.getPicture(_this.successCallback, _this.errorCallback, {
						sourceType: window.Camera.PictureSourceType.SAVEDPHOTOALBUM,
						saveToPhotoAlbum: true,
						destinationType: window.Camera.DestinationType.FILE_URI
					});
			}catch(e){
				console.log("BBB",e)
			}
			
		},
		successCallback(data){
			console.log('data',data);
		},
		errorCallback(data){
			console.log('data',data);
		},
		//开始录音
		recording() {
			console.log("SAAA")
			try{
				cordova.exec(this.success, this.fail, 'SpeechRecorder','startRecording', []);	
			}catch(e){
				console.log("SBBB",e)
			}
			
		},
		//结束录音
		stopRecording() {
			console.log("SCCC");
			try{
				cordova.exec(this.recordingSuccess, this.recordingSuccess, 'SpeechRecorder','stopRecording', []);	
			}catch(e){
				console.log("SDDD",e);
			}
			
		},
		success(result){
			console.log("A",result);
		},
		
		fail(error){
			console.log("B",error);
		},
		//录音成功回调
		recordingSuccess(result){
			console.log("录音成功 = " + result);
		},
		//录音失败回调
		recordingFail(error){
			console.log("C",error);
		},
		//获取地址位置
		getPosition(){
			 var options = {
		      enableHighAccuracy: true,
		      maximumAge: 3600000
		   }
			
		   var watchID = navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
		
		   function onSuccess(position) {
		
		      alert('Latitude: '          + position.coords.latitude          + '\n' +
		         'Longitude: '         + position.coords.longitude         + '\n' +
		         'Altitude: '          + position.coords.altitude          + '\n' +
		         'Accuracy: '          + position.coords.accuracy          + '\n' +
		         'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
		         'Heading: '           + position.coords.heading           + '\n' +
		         'Speed: '             + position.coords.speed             + '\n' +
		         'Timestamp: '         + position.timestamp                + '\n');
		   };
		
		   function onError(error) {
		      alert('code: '    + error.code    + '\n' + 'message: ' + error.message + '\n');
   			}
		}
	}
	
}
</script>